﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary1.DAL;

namespace ClassLibrary1.BLL
{
    public class Transaction
    {
        public TransactionHistoryDataContext db = new TransactionHistoryDataContext();
        public void Add(DateTime dtTransactionDate, decimal iAmount, string sDescription, string sSortCode, int iAccountNumber, string sType)
        {
            
            tTransaction oTransaction = new tTransaction();
            oTransaction.Account = db.Accounts.Single(a => a.AccountNumber == iAccountNumber && a.SortCode == sSortCode.Trim());
            TransactionType otransactiontype;
            otransactiontype = db.TransactionTypes.SingleOrDefault(tt => tt.Name == sType);
            if(otransactiontype == null)
            {
                otransactiontype = new TransactionType();
                otransactiontype.Name = sType;
            }
            oTransaction.TransactionType = otransactiontype;
            oTransaction.Description = sDescription;
            //TODO: if description contains an account number then add a destination account
            oTransaction.Date = dtTransactionDate;
            oTransaction.Amount = iAmount;
            db.tTransactions.InsertOnSubmit(oTransaction);
            db.SubmitChanges();
        }

        public tTransaction GetOldestNonCategorised()
        {
            return db.tTransactions.SingleOrDefault(t => t.TransactionID == db.tTransactions.Where(tr => tr.TransactionCategories.Count == 0).Min(ttr => ttr.TransactionID));
            
        }

        public List<string> getCategories()
        {
            List<string> lstCategories = new List<string>();
            foreach(Category cat in db.Categories)
            { 
                lstCategories.Add(cat.Name);
            }
            return lstCategories;
        }
        public void newCategory(string sName)
        {
            Category oCategory = new Category();
            oCategory.Name = sName;
            db.Categories.InsertOnSubmit(oCategory);
            db.SubmitChanges();
        }
        public void AddCategory(int iTransactionID, string sName)
        {
            TransactionCategory otranscat = new TransactionCategory();
            otranscat.CategoryID = db.Categories.SingleOrDefault(c => c.Name == sName).CategoryID;
            otranscat.TransactionID = iTransactionID;
            db.TransactionCategories.InsertOnSubmit(otranscat);
            db.SubmitChanges();
        }


        public bool Exists(DateTime TransactionDate, decimal Amount, string Description, string SortCode, int AccountNumber, string Type)
        {
            if (db.tTransactions.Count(
                    t => t.Date == TransactionDate &&
                    t.Amount == Amount &&
                    t.Description == Description &&
                    t.Account.SortCode == SortCode.Trim() &&
                    t.Account.AccountNumber == AccountNumber &&
                    t.TransactionType.Name == Type
                    ) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
           
        }

        public decimal TotalForCategory(string Category)
        {
            return db.TransactionCategories.Where(tc => tc.Category.Name == Category).Sum(tc => tc.tTransaction.Amount);
        }
    }
}
